package 公司_问答;

import Utils.HttpConnect;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.*;

public class 问答信息 {
    /**
     * 抓取各个公司的问答信息
     *
     * @param args
     */

    static final String COOKIE = "";
    static Map<String, String> header_map = new HashMap<>();
    static MongoCollection collection = new MongoClient("192.168.1.173", 27017).getDatabase("IRMB_上证e").getCollection("WenDaInfo");
    static MongoCollection urlCollection = new MongoClient("192.168.1.173", 27017).getDatabase("IRMB_上证e").getCollection("url");
    static Map<String, Integer> urlMap = inDbUrlList();

    static {
        /**
         * 初始化request.header
         */
        header_map.put("Cookie", COOKIE);
        header_map.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    }

    public static void main(String[] args) {
        List<org.bson.Document> companyList = getDbInfo();
        companyList.forEach(doc -> {
            String uid = doc.getString("url").replace("user.do?uid=", "");
            String url = "https://sns.sseinfo.com/ajax/userfeeds.do?typeCode=company&type=11&pageSize=10&uid=" + uid + "&page=";
            try {
                requestUrl(url, 1, 1, doc);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    static Map<String, Integer> inDbUrlList(){
        FindIterable<org.bson.Document> result = urlCollection.find();
        Map<String, Integer> urlMap = new LinkedHashMap<>();

        for (org.bson.Document doc : result)
            urlMap.put(doc.getString("_id"), (Integer) doc.get("docIndex"));
        return urlMap;
    }

    static List<org.bson.Document> getDbInfo() {
        /**
         * 从数据库中获取公司信息
         */
        MongoCollection companyInfoCollection = new MongoClient("192.168.1.173", 27017).getDatabase("IRMB_上证e").getCollection("CompanyInfo");
        FindIterable<org.bson.Document> result = companyInfoCollection.find();

        List<org.bson.Document> resList = new ArrayList<>();

        for (org.bson.Document doc : result)
            resList.add(doc);

        return resList;
    }

    static void requestUrl(String url, int pageIndex, int docIndex, org.bson.Document companyDoc) throws InterruptedException {
        if(urlMap.containsKey(url + String.valueOf(pageIndex))){
            System.out.println("已经抓取：   " + url + String.valueOf(pageIndex));
            requestUrl(url, pageIndex+1, urlMap.get(url + String.valueOf(pageIndex)), companyDoc);
            return;
        }

        Thread.sleep(500);
        String responseStr = HttpConnect.HttpGet_OkHttp(url + String.valueOf(pageIndex), header_map);

        /**
         * 判断是否结束
         */
        if (responseStr.contains("暂时没有问答"))
            return;

        List<org.bson.Document> docList = parseHTML(responseStr);
        for (org.bson.Document doc : docList) {
            doc.append("股票代码", companyDoc.getString("companyId"));
            doc.append("公司简称", companyDoc.getString("companyName"));
            doc.append("序号", String.valueOf(docIndex++));
            doc.append("_id", doc.getString("股票代码") + "_" + doc.getString("序号"));

            BasicDBObject obj = new BasicDBObject("$set", doc);
            BasicDBObject query = new BasicDBObject().append("_id", doc.get("_id"));
            collection.updateOne(query, obj, (new UpdateOptions()).upsert(true));
            System.out.println(doc.get("_id") + " 写入数据库");
        }
        urlCollection.insertOne(new org.bson.Document("_id",url + String.valueOf(pageIndex))
                                                     .append("docIndex", docIndex));
        requestUrl(url, pageIndex+1, docIndex, companyDoc);
    }

    static List<org.bson.Document> parseHTML(String responseStr) {
        Document doc = Jsoup.parse(responseStr);
        Elements tagDivList = doc.getElementsByClass("m_feed_item");

        List<org.bson.Document> resList = new ArrayList<>();
        int lenOfTagDiv = tagDivList.size();
        for (int i = 0; i < lenOfTagDiv; i++) {
            Element tagDiv = tagDivList.get(i);
            String html_id= tagDivList.attr("id");
            String userName = tagDiv.getElementsByClass("m_feed_face").get(0).text();
            String userType = userName.contains("浏览用户") ? "浏览用户" : "注册用户";
            String askDate = tagDiv.getElementsByClass("m_feed_func").get(0).getElementsByTag("span").first().text();
            String askInfo = tagDiv.getElementsByClass("m_feed_txt").get(0).text();

            String isAnswer = "未回复", answerDate = "", answerInfo = "";
            if(tagDiv.getElementsByClass("m_feed_detail").size() == 2){
                isAnswer = "已回复";
                answerDate = tagDiv.getElementsByClass("m_feed_detail").get(1).getElementsByClass("m_feed_from").first().getElementsByTag("span").text();
                answerInfo = tagDiv.getElementsByClass("m_feed_detail").get(1).getElementsByClass("m_feed_txt").text();
            }

            resList.add(new org.bson.Document("浏览用户/注册用户", userType)
                                     .append("用户名", userName)
                                     .append("提问时间", askDate)
                                     .append("提问内容", askInfo)
                                     .append("上市公司是否回复", isAnswer)
                                     .append("回复时间", answerDate)
                                     .append("回复内容", answerInfo)
                                     .append("Html_id", html_id));
        }


        return resList;
    }
}
